package br.com.edumobi.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import br.com.edumobi.P;

public abstract class Dates {

    public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String FORMAT_HH_MM = "HH:MM";

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", P.LOCALE_DEFAULT);

    public static String dateToString(Date date) {
        if (date != null) {
            return dateFormat.format(date);
        } else {
            return null;
        }
    }

    public static String dateToString(Date date, String format) {
        if (date != null) {
            if (format != null || !format.trim().equals("")) {
                SimpleDateFormat dateFormat = new SimpleDateFormat(format, P.LOCALE_DEFAULT);
                return dateFormat.format(date);
            }
        }
        return null;
    }

    public static Date stringToDate(String string) {
        if (string != null && !string.isEmpty()) {
            try {
                return dateFormat.parse(string);
            } catch (ParseException excecao) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static Date stringToDate(String string, String format) {
        if (string != null && !string.isEmpty()) {
            if (format != null && !format.isEmpty()) {
                SimpleDateFormat dateFormat = new SimpleDateFormat(format, P.LOCALE_DEFAULT);
                try {
                    return dateFormat.parse(string);
                } catch (ParseException excecao) {
                    return null;
                }
            }
        }
        return null;
    }
}
